<?php
//saads
Class DataModel{
	//loaded values from DB or not? If yes, Save will do a update, if not, a insert
	private $dbLoaded = false;
	private $dbLink= NULL;
	protected $primaryKey="";
	
	public function __construct($defautValues = NULL){
		$this->dbLink = DB::getInstance();
		//$this->dbLoaded = $dbLoaded;
		//echo get_class($this);
		if($defautValues){
			$this->dbLoaded = true;
			foreach($defautValues as $key=>$value){
				$this->$key = $value;
			}
		}
	}
	
	public function ToString(){
		$str = "" ;
		foreach($this->GetColumns() as $column){
			$str.= "$column: ".$this->$column.", ";
		}
		return $str;
	}
	
	//TODO:deletes object from db
	public function Delete($flagOnly= false){
		if ($this->primaryKey){
			//delete from table where pk = id
			$pkName = $this->primaryKey;
			$pkValue =  DB::EscapeValue($this->$pkName);
			$query = "delete from ".$this->GetTableName(). " where ". $this->primaryKey. " = ".$pkValue ;
			
			$this->dbLink->execute($query);
			//echo $query;
		}return false;
	}
	
	//TODO: Make static:
	public function GetTableName(){
		return get_class($this);
	}
	
	//saves self on db
	public function Save(){
		
		$columns = Array();
		$values = Array();
		$columnValues= Array();
		foreach($this->GetColumns() as $key){
			//echo $key."=".$this->$key."<br>";
			$columns[$key] = $key;
			if (is_null($this->$key)){
				$value = "null";
			}else{
				$value = DB::EscapeValue($this->$key); //"'" . str_replace("'",DB::$escapeChar,$value) . "'";
			}
			$values[$key] = $value;
			$columnValues[] = "$key = $value";
		
		}
		$tableName = get_class($this);
		if ($this->dbLoaded){
			$query = "update ".$tableName." set ".implode($columnValues,",")." where " . $this->primaryKey. " = ".$values[$this->primaryKey]." ;";
			$this->dbLink->execute($query);
			//echo $query;
		}else{
			$query = "insert into ".$tableName."(".implode($columns,",").") values (" .implode($values,","). ");";
			//echo $query;
			$this->dbLink->execute($query);
			//echo $query;
		}
		
		if ($this->primaryKey){
			//$this->id = $this->dbLink->lastInsertedId();
			$pk = ($this->primaryKey);
			$this->$pk = $this->dbLink->lastInsertedId();
		}
		$this->dbLoaded = true;
	}
	
	//TODO: Make this static private
	public function GetColumns(){
		$columns = Array();
		$baseModel = new DataModel();
		foreach($this as $key=>$value){
			if (!isset($baseModel->$key)){
				$columns[] = $key;
			}
		}
		
		return $columns;
	}
	
	//TODO: Make this static
	//TODO: Verify if need to pass primary key information
	public function CreateJsModel(){
		$classAttributes=Array();
		$classAttributes[] = "__className:'".get_class($this)."'";
		foreach($this->GetColumns() as $column){
			$classAttributes[]="$column:''";
		}
		return get_class($this) ." = DataModel.extend({\n\t".implode($classAttributes,",\n\t")."\n});\n\n";
		
	}
	
	//TODO:
	public function CreateTableCode(){
		$query = "
		CREATE TABLE IF NOT EXISTS `mymodel` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `text` text NOT NULL,
		  PRIMARY KEY (`id`)
		)
		";
	}
}


?>
